//
//  WordActionCell.m
//  Learn
//
//  Created by chijianguo on 2024/4/10.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "WordActionCell.h"
@interface WordActionCell ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSUInteger count;

@end

@implementation WordActionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    
}

- (void)startRecording{
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    self.count = 50;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [self addRing:(50-self.count)/50.0];
        self.count = self.count - 1;
        if(self.count <= 0){
            [self stopRecord];
        }
    }];
    [self.contentView.layer addSublayer:self.ringLayer];
    [self.recordBtn setImage:[UIImage imageNamed:@"luyining"] forState:(UIControlStateNormal)];
}

- (void)stopRecord {
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    [self.ringLayer removeFromSuperlayer];
    [self.recordBtn setImage:[UIImage imageNamed:@"luyin"] forState:(UIControlStateNormal)];
}

- (void)addRing:(CGFloat)value {
    self.ringLayer.strokeEnd = value;
}

- (CAShapeLayer *)ringLayer {
    if (_ringLayer == nil) {
        _ringLayer = [[CAShapeLayer alloc] init];
        _ringLayer.strokeColor = COLOR_TOPIC_SELECT.CGColor;
        CGRect frame = self.recordBtn.frame;
        _ringLayer.frame = frame;
        CGFloat w = frame.size.width;
        UIBezierPath * circleP = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-1, -1, w+2, w+2)];
        _ringLayer.path           = circleP.CGPath;
        _ringLayer.lineWidth      = 4;
        _ringLayer.strokeStart    = 0;
        _ringLayer.strokeEnd      = 0;
        _ringLayer.fillColor      = [UIColor clearColor].CGColor;
        _ringLayer.fillRule       = kCAFillRuleEvenOdd;
    }
    return _ringLayer;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setWordModel:(TextBookUnitWordModel *)wordModel {
    _wordModel = wordModel;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fileName = [NSString stringWithFormat:@"%@.pcm",wordModel.wId];
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];
    NSData *data = [NSData dataWithContentsOfFile:path];
    DebugLog(@"----%@==========",data);
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:path]) {
        self.playBtn.hidden = NO;
    }else {
        self.playBtn.hidden = YES;
    }

}

- (IBAction)recordAction:(UIButton *)sender {
    if(self.timer){
        return;
    }
    if(self.recordBlock){
        self.recordBlock();
    }
    [self startRecording];
}
- (IBAction)lessonAction:(UIButton *)sender {
    [self stopRecord];
    if(self.lessonBlock){
        self.lessonBlock();
    }

}
- (IBAction)playAction:(UIButton *)sender {
    [self stopRecord];
    if(self.playBlock){
        self.playBlock();
    }

}

- (IBAction)preAction:(UIButton *)sender {
    [self stopRecord];
    if(self.preBlock){
        self.preBlock();
    }

}
- (IBAction)nextAction:(UIButton *)sender {
    [self stopRecord];
    if(self.nextBlock){
        self.nextBlock();
    }

}
@end
